/**
 * @Title: login
 * @Package mall-fe
 * @author Shinelon
 * @date 2017/9/3 13:23
 * @version V1.0
 */
"use strict";
require('./index.css');
require('js/common/nav-simple/index');
var shiqla = require('util/shiqlamall');
var userService = require('../../service/user-service');
/**
 * 登录页面逻辑
 * @type {{init: page.init}}
 */
var formError = {
    show : function(errMsg){
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide : function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
};

var page = {
    init:function () {
        this.bindEvent();
    },
    bindEvent:function () {
        var _this = this;
        $('#username').blur(function () {
            var username = $.trim($('#username').val());
            //如果用户名为空,则不需要验证
            if(!username){
                return ;
            }
            userService.checkUsername(username,function (result) {
                formError.hide();
            },function (errMsg) {
                formError.show(errMsg);
            });
        });

        $('#submit').click(function () {
            _this.submit();
        });
        $('.user-content').keyup(function (e) {
            if(e.keyCode === 13){
                _this.submit();
            }
        });
    },
    //提交登录数据
    submit:function () {
        var formData = {
            username:$.trim($('#username').val()),
            password:$.trim($('#password').val()),
            passwordConfirm:$.trim($('#password-confirm').val()),
            phone:$.trim($('#phone').val()),
            email:$.trim($('#email').val()),
            question:$.trim($('#question').val()),
            answer:$.trim($('#answer').val())

        };
        var validateResult = this.formValidate(formData);
        if(validateResult.status){
            //提交登录
            userService.register(formData,function (data,msg) {
                window.location.href = './result.html?type=register';
            },function (err) {
                formError.show(err);
            });
        }else{
            //失败,错误提示
            formError.show(validateResult.msg);
        }
    },
    //表单验证
    formValidate:function (formData) {
        var result = {
            status: false,
            msg:''
        };
        if(!shiqla.validate(formData.username,'require')){
            result.msg = '用户名不能为空';
            return result;
        }
        if(!shiqla.validate(formData.password,'require')){
            result.msg = '密码不能为空';
            return result;
        }
        if(formData.password.length < 6){
            result.msg = '密码不能小于6位';
            return result;
        }
        if(formData.password !== formData.passwordConfirm){
            result.msg = '两次输入的密码不一致';
            return result;
        }
        if(!shiqla.validate(formData.phone,'phone')){
            result.msg = '手机格式不正确';
            return result;
        }
        if(!shiqla.validate(formData.email,'email')){
            result.msg = '邮箱地址不正确';
            return result;
        }
        if(!shiqla.validate(formData.question,'require')){
            result.msg = '提示问题不能为空';
            return result;
        }
        if(!shiqla.validate(formData.answer,'require')){
            result.msg = '提示问题答案不能为空';
            return result;
        }
        //通过验证,返回正确数据
        result.status = true;
        result.msg = '验证通过';
        return result;
    }
};
$(function () {
    page.init();
});